// pages/user_info/my_order/my_order.js
const { request, PageContent } = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var wxcache = require('../../../utils/wxcache.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navTabs: ["全部订单", "待付款", "待收货","待退款","未退款","已退款"],
        tabCur: 0, //默认选中
        scrollLeft:0,
        tabs: [{
          name: '全部订单',
          id: 0
        },
        {
          name: '待付款',
          id: 1
        },
        {
          name: '待收货',
          id: 2
        },
        {
          name: '待退款',
          id: 3
        },
        {
          name: '未退款',
          id: 4
        },
        {
          name: '已退款',
          id: 5
        }
        ],
        // 初始化将activeIndex置为空
        activeIndex: "",
        // 初始化支付成功list，置为空数组
        paidOrder: [],
        // 初始化待支付订单list，置为空数组
        unpaidOrder: [],
        //初始化发起退款订单list，置为空数组
        startRefundOrder:[],
        failedRefundOrder:[],//退款失败
        successRefundOrder:[],//退款成功
        //初始化所有订单list,置为空数组
        allOrder: [],
        show_data:[],
        data:[
          {
            order_id:1,
            order_status:0,
            order_price:20,
            order_goods:[
              {  img_url:"http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity:1
              },
              {
                img_url: "http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity: 2
              },
              {
                img_url: "http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity: 2
              }
            ]
          },
          {
            order_id: 2,
            order_status: 0,
            order_price: 30,
            order_goods: [
              {
                img_url: "http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity: 1
              },
              {
                img_url: "http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity: 2
              },
              {
                img_url: "http://img.51miz.com/Element/00/83/77/33/843a123e_E837733_e92b9cac.png!/quality/90/unsharp/true/compress/true/format/png/fh/630",
                goods_quantity: 2
              }
            ]
          }
        ]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var _this = this;
      const that = this;
      var uid = wxcache.get("userInfo").id;
      var url = api.goodOrderList;
      var data = {
        startIndex : 1,
        pageSize : 500,
        userId: uid,
        bizType:2
      };
      wx.request({
        url: url,
        method: "GET",
        data: data,
        success: function (res) {
          if (res.data.message == "ok"){
            var orderDetailList = res.data.responseBody.orderDetailList;
            console.log("从后端获取所有订单："+orderDetailList);
            var waitPayArr = [];
            var paySuccessArr = [];
            var startRefundArr = [];
            var failedRefundArr = [];
            var successRefundArr = [];
            for (var i = 0; i < orderDetailList.length; i++){
              if((orderDetailList[i].status == "WAIT_PAY")||
                (orderDetailList[i].status == "PAYING")){
                waitPayArr.push(orderDetailList[i]);
              }
              if(orderDetailList[i].status == "PAY_SUCCESS"){
                paySuccessArr.push(orderDetailList[i]);
              }
              if ((orderDetailList[i].status == "START_REFUND") || 
                  (orderDetailList[i].status == "CUSTOMER_SUCCESS") || 
                  (orderDetailList[i].status == "REFUNDING")) {
                startRefundArr.push(orderDetailList[i]);
              }
              if ((orderDetailList[i].status == "CUSTOMER_REJECT") || 
                  (orderDetailList[i].status == "REFUND_FAILED")){
                failedRefundArr.push(orderDetailList[i]);
              }
              if (orderDetailList[i].status == "REFUND_SUCCESS") {
                successRefundArr.push(orderDetailList[i]);
              }
            }
            _this.setData({
              allOrder: orderDetailList, //全部订单
              unpaidOrder: waitPayArr, //待付款
              paidOrder: paySuccessArr, //待收货
              startRefundOrder:startRefundArr,//发起退款,退款中
              failedRefundOrder:failedRefundArr,//退款失败
              successRefundOrder:successRefundArr//退款成功
            })
          }
        }
      });

      if (options.activeIndex == undefined){
        this.setData({
          activeIndex: 0
        });
      }else{
        this.setData({
          // 此处获取的activeIndex为user_info页面navigator传过来的参数
          activeIndex: options.activeIndex
        });
      }
    },
    /**
     * nav item的tap事件
     */
    tabSelect: function (e) {
        this.setData({
          activeIndex: e.currentTarget.id,
          tabCur: e.currentTarget.dataset.id,
          scrollLeft: (e.currentTarget.dataset.id - 2) * 200
        });
    },

  /**
   * tap当前订单去当前订单详情页
   */
  showOrderDetail: function (e) {
    // 获取当前订单的ID作为值传入url中
    const order_id = e.currentTarget.id;
    PageContent.loadNavigate(`../order_detail/order_detail?order_id=${order_id}`)
  },

  /**
   * 退款
   */
  refund: function(event){
    let songid = event.currentTarget.dataset.songid;
    var that = this;
    var url = api.refund;
    var data = { orderId: songid};
    wx.showModal({
      title: '',
      content: '是否要进行退款',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定退款')
          wx.request({
            url: url,
            data: data,
            method: "POST",
            success: function (res) {
              if (res.data.responseBody == "success") {
                wx.showModal({
                  title: '',
                  content: '退款申请已提交，请耐心等待！',
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  }
});